//
//  BQTabBarController.m
//  BeeQuick
//
//  Created by mac on 16/11/18.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQTabBarController.h"
#import "BQNavigationController.h"
@interface BQTabBarController ()

@end

@implementation BQTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // MARK: 接收购物车Model里的数量
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buyCarNum:) name:@"change" object:nil];
    
    //MARK: - 接受购物车的通知.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(goHomePage:) name:@"goShoping" object:nil];
    
    NSDictionary *dicFirst = @{@"vcName":@"BQHomeViewController",@"title":@"首页",@"img":@"Tb_home"};
    NSDictionary *dicSecond = @{@"vcName":@"BQMarketViewController",@"title":@"闪送超市",@"img":@"Tb_order"};
    NSDictionary *dicThird = @{@"vcName":@"BQBuyCarViewController",@"title":@"购物车",@"img":@"Tb_shopCart"};
    NSDictionary *dicFourth = @{@"vcName":@"BQMineViewController",@"title":@"我的",@"img":@"Tb_my"};
    UIViewController *vcFirst = [self creatMyControllerWithDic:dicFirst];
    UIViewController *vcSecond = [self creatMyControllerWithDic:dicSecond];
    UIViewController *vcThird = [self creatMyControllerWithDic:dicThird];
    UIViewController *vcFourth = [self creatMyControllerWithDic:dicFourth];
    
    
    self.viewControllers = @[vcFirst,vcSecond,vcThird,vcFourth];
    
    
    //设置tabBar栏选中 图标和字的渲染颜色
//    [[UITabBar appearance] setTintColor:[UIColor yellowColor]];
    
    //
    //MARK: - 设置tabBar的title的选中颜色.
    [[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateSelected];
    
    
    
    
    
    
}


#pragma mark - 接受购物车传过来的 跳转到首页
- (void)goHomePage:(NSNotification *)notification
{
    
    [self setSelectedIndex:0];
    
}
#pragma mark - 封装的一个方法 创建子控制器
- (UIViewController*)creatMyControllerWithDic:(NSDictionary *)dic
{
    Class clz = NSClassFromString(dic[@"vcName"]);
    
    UIViewController *vc = [[clz alloc]init];
    
//    vc.title = dic[@"title"];
    
    
    vc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",dic[@"img"]]];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_r",dic[@"img"]]];
    
#warning 如果你没在 Assets.xcassets 这里设置图片的 渲染色的话可以实现代码 设置
    
    //     vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",dic[@"img"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    vc.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@",dic[@"img"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    BQNavigationController *naVc = [[BQNavigationController alloc]initWithRootViewController:vc];
    
    
    //MARK: - 单独设置TabBar栏上的文字
     naVc.tabBarItem.title = dic[@"title"];
    
    return naVc;
}

- (void)buyCarNum:(NSNotification *)no
{
    
    self.viewControllers[2].tabBarItem.badgeValue = @([myCar showTotalNum]).description;
    
    if([myCar showTotalNum] == 0)
    {
        self.viewControllers[2].tabBarItem.badgeValue = nil;
    }
    
}


@end
